package interview.stack;

import java.util.Stack;

/**
 * 最小栈的实现
 */
public class MinStack {
    //主栈
    private static Stack<Integer> mainStack = new Stack<Integer>();
    //副栈
    private static Stack<Integer> minStack = new Stack<Integer>();

    /**
     * 入栈操作
     * @param element 入栈目标值
     */
    public void push(Integer element){
        mainStack.push(element);
        //对比如果 小栈为空或者小栈的栈顶大于当前入栈数据 小栈入栈
        if (minStack.empty() || element <= minStack.peek()){
            minStack.push(element);
        }
    }

    /**
     * 出栈
     * @return 出栈的元素
     */
    public Integer pop(){
        if (mainStack.peek() == minStack.peek()){
            minStack.pop();
        }
        return mainStack.pop();
    }

    /**
     * 获取栈中最小元素
     * @return 返回最小元素
     */
    public Integer getMin(){
        if (mainStack.empty()){
            return null;
        }
        return minStack.peek();
    }
}
